<template>
	<view class="container">
		<u-form :model="form" ref="uForm" label-position="top" :error-type="['toast']">
			<u-form-item label="选择位置" prop="targetName">
				<u-input v-model="form.targetName" type="select" @click="show = true"/>
			</u-form-item>
			<u-form-item label="照片" prop="date">
				<x-upload
					ref="uUpload"
					:form-data="formData"
					@onUploaded="onUploaded"
					:hasVideo="false"
				/>
			</u-form-item>
			<u-form-item label="其他说明" prop="content">
				<u-input v-model="form.content" type="textarea"/>
			</u-form-item>
		</u-form>
		<u-button class="action-view" @click="saveClick">保存</u-button>
		<u-select v-model="show" :list="list" label-name="name" value-name="id" @confirm="confirm"></u-select>
	</view>
</template>

<script>
	import { getAllArea, addSterilization } from '@/api/xj.js'
	import XUpload from '@/components/x-upload'
	import {
		mapState
	} from 'vuex'
	export default {
		components: {
			XUpload
		},
		data() {
			return {
				form: {
					targetName: '',
					targetId: '',
					targetType: '0',
					content: ''
				},
				rules: {
					targetId: [{
						required: true,
						message: '请选择位置',
					}, ],
					content: [{
						required: true,
						message: '请填写说明',
					}, ],
				},
				show: false,
				list: [
				],
				
				formData: {
					'bizType': 'record-sterilization'
				},
			}
		},
		computed: {
			...mapState({
				tchInfo: state => state.user.tchInfo,
			})
		},
		onLoad() {
			getAllArea({pageNum: 0, pageSize: 999}).then(res => {
				console.log(res)
				this.list = res.data.content
			})
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			saveClick() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						this.addRecord();
						return;
					}
				});
			},
			
			addRecord() {
				this.$refs.uUpload.upload();
			},
			
			confirm(e) {
				console.log(e);
				this.form.targetId = e[0].value
				this.form.targetName = e[0].label
			},
			
			onUploaded(data) {
				this.form.resourceUrl = data.resourceUrl;
				this.form.mediaType = 1; 
				this.form.operatorId = this.tchInfo.staff.id
				this.form.operatorName = this.tchInfo.staff.realName
				addSterilization(this.form).then(res => {
					this.$u.toast('添加成功');
					setTimeout(function(){
						uni.navigateBack({delta:1})
					},1000)
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: $app-page-bg-color;
	}
</style>
<style lang="scss" scoped>
	@import '@/css/u-form.scss';
	
	.container {
		padding-left: 30rpx;
		padding-right: 30rpx;
		display: flex;
		flex-direction: column;
	}

	.children-view {
		width: 10rpx;
		flex: 1;
		display: flex;
		flex-direction: row;
	}
</style>
